package br.com.DAO;

import java.util.ArrayList;
import java.util.List;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import br.com.Bean.DisciplinaBean;
import br.com.Bean.UsuarioBean;

public class DisciplinaDAO {
	
	public void salva(DisciplinaBean disciplinaBean,UsuarioBean usuario){
		ParseObject disciplina = new ParseObject("Disciplina");
		disciplina.put("cargaHoraria", disciplinaBean.getCargaHoraria());
		disciplina.put("codigoAcesso", disciplinaBean.getCodigoAcesso());
		//disciplina.put("codigoDisciplina", disciplinaBean.getCodigoDisciplina());
		disciplina.put("instituicao", disciplinaBean.getIntituicao());
		disciplina.put("nome", disciplinaBean.getNome());
		//disciplina.put("professor", disciplinaBean.getUsuarioIdProfessor());
		disciplina.put("idUsuario", usuario.getId());
		disciplina.saveInBackground();
	}
	
	public void deleta(DisciplinaBean disciplinaBean){
		 List<ParseObject> todos = null;
		 List<DisciplinaBean> listaTodos = new ArrayList<>();
		
		ParseQuery query = new ParseQuery("Disciplina");
		query.whereEqualTo("objectId", disciplinaBean.getId());
		 try {
				todos = query.find();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		 if(todos != null){
			 for (ParseObject todo : todos) {
                DisciplinaBean d = new DisciplinaBean();
                	d.setId(todo.getObjectId());
                	todo.deleteInBackground();
                	System.out.println("DELETOU");
          //       listaTodos.add(d);
             }
		 }
	}
	
	public List<DisciplinaBean> buscaPorUsuario(UsuarioBean usuarioBean){
		 AlunoDAO alunoDAO = new AlunoDAO();
		 AulaDAO aulaDAO = new AulaDAO();
		 ForumDAO forumDAO = new ForumDAO();
		 
		 List<ParseObject> todos = null;
		 List<DisciplinaBean> listaTodos = new ArrayList<>();
		
		ParseQuery query = new ParseQuery("Disciplina");
		query.whereEqualTo("idUsuario", usuarioBean.getId());
		 try {
				todos = query.find();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		 if(todos != null){
			 for (ParseObject todo : todos) {
                DisciplinaBean d = new DisciplinaBean();
                	d.setCargaHoraria(todo.getInt("cargaHoraria"));
                	d.setCodigoAcesso(todo.getInt("codigoAcesso"));
                	d.setCodigoDisciplina(todo.getInt("codigoDisciplina"));
                	d.setId(todo.getObjectId());
                	d.setIntituicao(todo.getString("instituicao"));
                	//d.setListaAlunos(alunoDAO.buscaPorIdDisciplina(todo.getObjectId()));
                	//d.setListaArquivos(arquivoDAO.buscaPeloIdDisciplina(todo.getObjectId()));
                	//d.setListaAulas(aulaDAO.buscaPeloIdDisciplina(todo.getObjectId()));
                	//d.setListaForum(forumDAO.buscaPeloIdDisciplina(todo.getObjectId()));
                	d.setNome(todo.getString("nome"));
                	d.setUsuarioProfessor(usuarioBean);
                 
                 listaTodos.add(d);
                // System.out.println("TAMANO:" + listaTodos.size());
             }
		 }
		 
		return listaTodos; 
	}
	
	public List<DisciplinaBean> listaDisciplinaBuscaInstituicao(String insituicao){
		 UsuarioDAO usuarioDAO = new UsuarioDAO();
		 List<ParseObject> todos = null;
		 List<DisciplinaBean> listaTodos = new ArrayList<>();
		
		ParseQuery query = new ParseQuery("Disciplina");
		query.whereEqualTo("instituicao", insituicao);
		 try {
				todos = query.find();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		 if(todos != null){
			 for (ParseObject todo : todos) {
               DisciplinaBean d = new DisciplinaBean();
               	d.setCargaHoraria(todo.getInt("cargaHoraria"));
               	d.setCodigoAcesso(todo.getInt("codigoAcesso"));
               	d.setCodigoDisciplina(todo.getInt("codigoDisciplina"));
               	d.setId(todo.getObjectId());
               	d.setIntituicao(todo.getString("instituicao"));
               	d.setNome(todo.getString("nome"));
               	d.setUsuarioProfessor(usuarioDAO.buscaPeloId(todo.getString("idUsuario")));
                
                listaTodos.add(d);
            }
		 }
		 
		return listaTodos;
	}
	
	public List<DisciplinaBean> listaDisciplinaBuscaCodigo(String codigo){
		 UsuarioDAO usuarioDAO = new UsuarioDAO();
		 List<ParseObject> todos = null;
		 List<DisciplinaBean> listaTodos = new ArrayList<>();
		
		ParseQuery query = new ParseQuery("Disciplina");
		query.whereEqualTo("objectId", codigo);
		 try {
				todos = query.find();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		 if(todos != null){
			 for (ParseObject todo : todos) {
               DisciplinaBean d = new DisciplinaBean();
               	d.setCargaHoraria(todo.getInt("cargaHoraria"));
               	d.setCodigoAcesso(todo.getInt("codigoAcesso"));
               	d.setCodigoDisciplina(todo.getInt("codigoDisciplina"));
               	d.setId(todo.getObjectId());
               	d.setIntituicao(todo.getString("instituicao"));
               	d.setNome(todo.getString("nome"));
               	d.setUsuarioProfessor(usuarioDAO.buscaPeloId(todo.getString("idUsuario")));
                
                listaTodos.add(d);
            }
		 }
		 
		return listaTodos;
	}
	
	public DisciplinaBean buscaDisciplinaPorCodigo(String codigo){
		 UsuarioDAO usuarioDAO = new UsuarioDAO();
		 List<ParseObject> todos = null;
		 List<DisciplinaBean> listaTodos = new ArrayList<>();
		 DisciplinaBean disciplina = new DisciplinaBean();
		
		ParseQuery query = new ParseQuery("Disciplina");
		query.whereEqualTo("objectId", codigo);
		 try {
				todos = query.find();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		 if(todos != null){
			 for (ParseObject todo : todos) {
               DisciplinaBean d = new DisciplinaBean();
               	d.setCargaHoraria(todo.getInt("cargaHoraria"));
               	d.setCodigoAcesso(todo.getInt("codigoAcesso"));
               	d.setCodigoDisciplina(todo.getInt("codigoDisciplina"));
               	d.setId(todo.getObjectId());
               	d.setIntituicao(todo.getString("instituicao"));
               	d.setNome(todo.getString("nome"));
               	d.setUsuarioProfessor(usuarioDAO.buscaPeloId(todo.getString("idUsuario")));
                
                listaTodos.add(d);
            }
		 }
		 
		return listaTodos.get(0);
	}
}
